package com.djhu.msg.annotation;

import org.springframework.beans.BeansException;
import org.springframework.beans.MutablePropertyValues;
import org.springframework.beans.PropertyValue;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.config.BeanDefinitionVisitor;
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.util.StringValueResolver;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * Created by zw on 2018/3/16.
 */
public class ObsceRemovingBeanFactoryBean implements BeanFactoryPostProcessor {
    private Set<String> values = new HashSet<>();

    public Set<String> getValues() {
        return values;
    }

    public void setValues(Set<String> values) {
        this.values = values;
    }

    @Override
    public void postProcessBeanFactory(ConfigurableListableBeanFactory configurableListableBeanFactory) throws BeansException {
        String[] beannames = configurableListableBeanFactory.getBeanDefinitionNames();
        for (String beanName: beannames){
            BeanDefinition beanDefinition = configurableListableBeanFactory.getBeanDefinition(beanName);

//            MutablePropertyValues pro =beanDefinition.getPropertyValues();
//            List<PropertyValue> alus = pro.getPropertyValueList();
//            for (PropertyValue v:alus){
//                v.getName();
//            }

            StringValueResolver stringValueResolver = new StringValueResolver() {
                @Override
                public String resolveStringValue(String strVal) {
                    if (values.contains(strVal)){
                        return "******";
                    }
                    return strVal;
                    //return null;
                }
            };
            BeanDefinitionVisitor visitor = new BeanDefinitionVisitor(stringValueResolver);
            visitor.visitBeanDefinition(beanDefinition);
        }

    }




}